package com.itheima.service.impl;

import com.itheima.common.utils.DateUtils;
import com.itheima.common.utils.POIUtils;
import com.itheima.converter.OrderConverter;
import com.itheima.dto.OrderSettingDTO;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.vo.OrderSettingVO;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.Month;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    /**
     * 导出运营数据
     * @param response
     */
    @Override
    public void exportBusinessData(HttpServletResponse response,String filename) {
              response.setContentType("application/vnd.ms-excel");
              String fileName = "templates/"+filename;
               //通过poi写入数据
               InputStream in=this.getClass().getClassLoader().getResourceAsStream(fileName);
                  // System.out.println(in);
               try {
                   assert in != null;
                   XSSFWorkbook excel = new XSSFWorkbook(in);
                   //写入excel表
                   ServletOutputStream outputStream = response.getOutputStream();
                   excel.write(outputStream);

                   outputStream.close();
                   excel.close();
               } catch (IOException e) {
                   throw new RuntimeException(e);
               }
    }
    /**
     * 根据日期修改可预约人数
     * @param orderSettingDTO
     */
    @Override
    public void updateNumberByOrderDate(OrderSettingDTO orderSettingDTO) {
        OrderSetting orderSetting = OrderConverter.orderSettingToOrderSettingDTO(orderSettingDTO);
        //根据日期查询数据是否存在
        List<OrderSetting> orderSettings = orderSettingMapper.selectByOrderDate(orderSetting);
        if (orderSettings!=null && orderSettings.size()>0) {
            //存在则更新数据
            orderSettingMapper.updateByOrderDate(orderSetting);
        }else {
            //不存在则插入数据
            orderSetting.setReservations(0);
            orderSettingMapper.insert(orderSetting);
        }

    }

    /**
     * 根据年月查询对应的预约设置信息
     * @param month
     * @return
     */
    @Override
    public List<OrderSettingVO> getOrderInfoByMonth(String month) throws Exception {
        Date date = DateUtils.parseString2Date(month, "yyyy-MM");
        LocalDate localDate = DateUtils.dateToLocalDate(date);
        int year =localDate.getYear();
        Month yearOfMonth = localDate.getMonth();
        List<OrderSetting> orderSettingsList = orderSettingMapper.getOrderSetting(year,yearOfMonth);
        List<OrderSettingVO> orderSettingVOList = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingsList) {
            OrderSettingVO orderSettingVO = OrderConverter.orderSettingToOrderSettingVO(orderSetting);
            Date orderDate = orderSetting.getOrderDate();
             LocalDate localDate1 = DateUtils.dateToLocalDate(orderDate);
            int day = localDate1.getDayOfMonth();
            orderSettingVO.setDate(day);
            orderSettingVOList.add(orderSettingVO);
        }
        return orderSettingVOList;
    }

    /**
     * 文件上传
     * @param excelFile
     */
    @Override
    public void upload(MultipartFile excelFile) {
        try {
            //解析文件
            List<String[]> list = POIUtils.readExcel(excelFile);
            List<OrderSetting> orderSettingList = new ArrayList<>();
            for (String[] strings : list) {
                String string = strings[0];
                String strings1=strings[1];
                Date orderDate =new Date(string);
                Integer number = Integer.valueOf(strings1);
            OrderSetting orderSetting = new OrderSetting(orderDate,number);
            orderSettingList.add(orderSetting);
            }
            //检查orderSetting是否存在
            if (orderSettingList!=null && orderSettingList.size()>0) {
                for (OrderSetting orderSetting : orderSettingList) {
                      //查询日期是否存在
                    List<OrderSetting> orderSettings = orderSettingMapper.selectByOrderDate(orderSetting);
                    if (orderSettings!=null && orderSettings.size()>0) {
                        //如果存在根据查询日期更新数据
                        orderSettingMapper.updateByOrderDate(orderSetting);
                    }else {
                        //如果不存在，则添加数据
                        orderSetting.setReservations(0);
                        orderSettingMapper.insert(orderSetting);
                    }
                }
            }
        } catch (IOException e) {
            throw new RuntimeException("文件解释失败");
        }
    }
}
